#testhotkey.py
import pythoncom, pyHook, ctypes

KEYDOWNID = [] #list to keep track of which keys are currently pressed


def isHotekey(hotkey):
    """Check if hotkey is pressed down
        Hotkey is given as KeyID"""

    try:            
        #make sure exact hotkey is pressed
        if not(len(hotkey) == len(KEYDOWNID)):
            return  False
        for hotk in hotkey:
            if not(hotk in KEYDOWNID):
                return False
    except:
        return False
    
    return True

#working
#=======
##def isHotekey(hotkey):
##    """Check if hotkey is pressed down
##        Hotkey is given as KeyID"""
##    ret = True
##
##    try:            
##        for hotk in hotkey:
##            if not(hotk in KEYDOWNID):
##                ret = False
##        #make sure exact hotkey is pressed
##        if not(len(hotkey) == len(KEYDOWNID)):
##            ret = False
##
##        print KEYDOWNID
##    except:
##        ret = False
##
##    return ret


#old
#====

##def isHotekey(hotkey):
##    """Check if hotkey is pressed down
##        Hotkey is given as KeyID"""
##    try:
##        #check if at least hotkey is pressed down
##        for hotk in hotkey:
##            if not(hotk in KEYDOWNID):
##                return False
##        #make sure exact hotkey is presse
####        if not(len(hotkey) == len(KEYDOWNID)):
####            return False                
##    except:
##        return False
##
##    return True

##def isHotekey(hotkey):
##    """Check if hotkey is pressed in any order
##        Hotkey is given as KeyID"""
##    ret = True
##
##    try:
##        if len(hotkey)<= len(KEYDOWNID):
##            for hotk in hotkey:
##                if not(hotk in KEYDOWNID):
##                    ret = False
##        else:
##            ret = False
##    except:
##        ret = False
##
##    return ret
##        
##    
 
def OnKeyDown(event):

    #make sure key only gets presse once
    if not(event.KeyID in KEYDOWNID):
        KEYDOWNID.append(event.KeyID)

        #Add Hotkeys here
        
        #test hotkey alt-f3, ALT = 164, F3 = 114, F2 113, 
        print "ALT F3", str(isHotekey([164,114]))

        #test hotkey ctrl = 162 shift = 160 3 = 51
        print "ctrl shift 3", str(isHotekey([162,160,51]))

        #quit PumpMessages loop
        if isHotekey([162,160,51]):
            ctypes.windll.user32.PostQuitMessage(0)
            
        print '---'

    return True

 
def OnKeyUp(event):
    KEYDOWNID.remove(event.KeyID)
    return True

 
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyDown
hm.KeyUp = OnKeyUp
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
